/*
 * AddMoneyBankDetailPanel.java
 *
 * Created on 2008年6月8日, 下午8:44
 */

package gui;

import abstractmodel.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import model.DetailInfo;

/**
 *
 * @author  littlechild
 */
public class AddMoneyBankDetailPanel extends javax.swing.JPanel {
    IAccount account;
    /** Creates new form AddMoneyBankDetailPanel */
    public AddMoneyBankDetailPanel(IAccount at) {
        account=at;
        initComponents();
        Date dateNow = new Date();
        DateFormat useTime=DateFormat.getDateInstance();
        useTime.getCalendar().setTime(dateNow);
        this.jTextFieldDate.setText(new SimpleDateFormat().format(dateNow));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelSet = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldDate = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldReason = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldAmount = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldLeave = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldOther = new javax.swing.JTextField();
        jButtonConfirm = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        jLabel1.setText("收支日期");

        jLabel2.setText("原因");

        jLabel3.setText("数额");

        jLabel4.setText("余额");

        jLabel5.setText("备注");

        jButtonConfirm.setText("确定");
        jButtonConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmActionPerformed(evt);
            }
        });

        jButtonCancel.setText("取消");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelSetLayout = new javax.swing.GroupLayout(jPanelSet);
        jPanelSet.setLayout(jPanelSetLayout);
        jPanelSetLayout.setHorizontalGroup(
            jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelSetLayout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelSetLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldLeave, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE))
                    .addGroup(jPanelSetLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldDate, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(22, 22, 22)
                .addGroup(jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelSetLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldOther, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelSetLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldReason, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jLabel3)
                        .addGap(19, 19, 19)
                        .addComponent(jTextFieldAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(96, 96, 96))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelSetLayout.createSequentialGroup()
                .addContainerGap(465, Short.MAX_VALUE)
                .addComponent(jButtonConfirm)
                .addGap(18, 18, 18)
                .addComponent(jButtonCancel)
                .addGap(48, 48, 48))
        );
        jPanelSetLayout.setVerticalGroup(
            jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelSetLayout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldReason, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(40, 40, 40)
                .addGroup(jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldLeave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldOther, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                .addGroup(jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonConfirm)
                    .addComponent(jButtonCancel))
                .addGap(27, 27, 27))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelSet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelSet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmActionPerformed
        // TODO add your handling code here:
        IDetail detail=Main.mf.newDetail(account);
        try {
            System.out.println("fe3trew");
            int fault;
            if (jTextFieldDate.getText().equals(""))
                fault = 1;
            else if (jTextFieldAmount.getText().equals(""))
                fault = 2;
            else if (jTextFieldReason.getText().equals(""))
                fault = 3;
            else if (jTextFieldOther.getText().equals(""))
                fault = 4;
            else 
                fault = 0;
            if (fault == 0 || fault == 4) {
                System.out.println("fault"+fault);
                DetailInfo di = new DetailInfo();
                if (!di.checkTime(jTextFieldDate.getText())) 
                    return;
                detail.getDetailInfo().setValue("收支日期", jTextFieldDate.getText());
                if (!jTextFieldLeave.getText().equals(""))
                    detail.getDetailInfo().setValue("余额", jTextFieldLeave.getText());
                detail.getDetailInfo().setValue("原因", jTextFieldReason.getText());
                detail.getDetailInfo().setValue("数额", jTextFieldAmount.getText());
                if (fault == 0)
                    detail.getDetailInfo().setValue("备注", jTextFieldOther.getText());
                else
                    System.out.println("other null!");
                account.addDetail(detail);
                jButtonCancelActionPerformed(null);
            }else {
                System.out.println("fault"+fault);
                String s;
                if(fault == 1)  s = "请填写收支日期!";
                else if(fault == 2)  s = "请填写交易数额！";
                else if(fault == 3)  s = "请填写原因！";
                else {
                    System.out.println("错误类型"+fault);
                    return;
                }
                String[] options = {"确定"};
                javax.swing.JOptionPane.showOptionDialog(null, s, 
                    "警告", javax.swing.JOptionPane.DEFAULT_OPTION, 
                    javax.swing.JOptionPane.WARNING_MESSAGE, null, options, options[0]); 
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }//GEN-LAST:event_jButtonConfirmActionPerformed

        private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // TODO add your handling code here:
        Utilities.cancelDialog(this);
}//GEN-LAST:event_jButtonCancelActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonConfirm;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanelSet;
    private javax.swing.JTextField jTextFieldAmount;
    private javax.swing.JTextField jTextFieldDate;
    private javax.swing.JTextField jTextFieldLeave;
    private javax.swing.JTextField jTextFieldOther;
    private javax.swing.JTextField jTextFieldReason;
    // End of variables declaration//GEN-END:variables
    
}
